# 基镜像
ARG BASE_IMAGE=ubuntu:20.04

# 拉取官方ubuntu 20.04镜像
FROM ${BASE_IMAGE}

# 在容器内使用bash
SHELL ["/bin/bash", "-ic"]

# 总体全局变量
# 依赖下载路径
ENV TARGET_DEPENDENCIES_PATH=/root/docker-config \
    # 依赖源路径
    SOURCE_DEPENDENCIES_PATH=./docker-config

# 从源路径复制依赖到目标路径
RUN mkdir -p "$TARGET_DEPENDENCIES_PATH"
COPY "$SOURCE_DEPENDENCIES_PATH" "$TARGET_DEPENDENCIES_PATH"

# 下载jq以解析json配置文件
RUN apt-get update -y && apt-get install -y jq

# 添加npu相关启动命令，添加devkit用户，修改devkit和root用户密码
ENV USER_TARGET_DEPENDENCIES_PATH="$TARGET_DEPENDENCIES_PATH"/common
RUN printf "# npu environment\nexport LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64/common:\$LD_LIBRARY_PATH\nexport LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64/stub:\$LD_LIBRARY_PATH\nexport LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64/driver:\$LD_LIBRARY_PATH\n" >> /etc/bash.bashrc && \
    for USER in $(jq -c '.users[]' "$USER_TARGET_DEPENDENCIES_PATH/user.json"); do \
        USERNAME=$(echo $USER | jq -r '.username'); \
        PASSWORD=$(echo $USER | jq -r '.password'); \
        if [ "$USERNAME" != "root" ]; then \
            useradd $USERNAME -m -s /bin/bash -d /home/$USERNAME; \
        fi; \
        echo "$USERNAME:$PASSWORD" > "$USER_TARGET_DEPENDENCIES_PATH"/"$USERNAME"_passwd.txt; \
        chpasswd < "$USER_TARGET_DEPENDENCIES_PATH"/"$USERNAME"_passwd.txt; \
    done

# 新建依赖路径，同时更新apt源，下载apt依赖
ENV APT_TARGET_DEPENDENCIES_PATH="$TARGET_DEPENDENCIES_PATH"/apt \
    # 关闭apt-get对图形化界面的调用
    DEBIAN_FRONTEND=noninteractive
RUN PACKAGES=$(jq -r '.PACKAGES[]' "$APT_TARGET_DEPENDENCIES_PATH/apt.json" | tr '\n' ' ') && \
    apt-get install -y --no-install-recommends $PACKAGES && \
    systemctl enable ssh && \
    service ssh start && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 安装Python
ENV PYTHON_TARGET_DEPENDENCIES_PATH="$TARGET_DEPENDENCIES_PATH"/python
RUN PYTHON_VERSION=$(jq -r '.PYTHON_VERSION' "$PYTHON_TARGET_DEPENDENCIES_PATH/python.json") && \
    PYTHON_MAJOR_VERSION=${PYTHON_VERSION%%.*} && \
    PYTHON_BASE_VERSION=$(echo $PYTHON_VERSION | cut -d. -f1,2) && \
    cd "$PYTHON_TARGET_DEPENDENCIES_PATH" && \
    PYTHON_SOURCE=$(jq -r '.PYTHON_SOURCE' "$PYTHON_TARGET_DEPENDENCIES_PATH/python.json") && \
    wget "$PYTHON_SOURCE/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.xz" && \
    tar -xf Python-"$PYTHON_VERSION".tar.xz && \
    cd Python-"$PYTHON_VERSION" && \
    ./configure --prefix=/usr/local/python"$PYTHON_VERSION" --enable-loadable-sqlite-extensions --enable-shared && \
    make && \
    make install && \
    printf "# python environment\nexport LD_LIBRARY_PATH=/usr/local/python%s/lib:\$LD_LIBRARY_PATH\nexport PATH=/usr/local/python%s/bin:\$PATH\n" "$PYTHON_VERSION" "$PYTHON_VERSION" >> /etc/bash.bashrc && \
    ln -sf /usr/local/python"$PYTHON_VERSION"/bin/python"$PYTHON_BASE_VERSION" /usr/bin/python && \
    ln -sf /usr/local/python"$PYTHON_VERSION"/bin/python"$PYTHON_BASE_VERSION" /usr/bin/python"$PYTHON_MAJOR_VERSION" && \
    ln -sf /usr/local/python"$PYTHON_VERSION"/bin/pip"$PYTHON_BASE_VERSION" /usr/bin/pip && \
    ln -sf /usr/local/python"$PYTHON_VERSION"/bin/pip"$PYTHON_BASE_VERSION" /usr/bin/pip"$PYTHON_MAJOR_VERSION" && \
    mkdir -p ~/.pip && \
    PIP_SOURCE=$(jq -r '.PIP_SOURCE' "$PYTHON_TARGET_DEPENDENCIES_PATH/python.json") && \
    TRUSTED_HOST=$(jq -r '.TRUSTED_HOST' "$PYTHON_TARGET_DEPENDENCIES_PATH/python.json") && \
    printf "[global]\nindex-url = $PIP_SOURCE\n[install]\ntrusted-host = $TRUSTED_HOST\n" > ~/.pip/pip.conf

# 下载安装python相关依赖
RUN source /etc/bash.bashrc && \
    pip install --upgrade pip && \
    pip install -r "$PYTHON_TARGET_DEPENDENCIES_PATH"/requirements.txt && \
    pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cpu && \
    wget -P "$PYTHON_TARGET_DEPENDENCIES_PATH" https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/aclruntime-0.0.2-cp39-cp39-linux_aarch64.whl --no-check-certificate && \
    wget -P "$PYTHON_TARGET_DEPENDENCIES_PATH" https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ais_bench-0.0.2-py3-none-any.whl --no-check-certificate && \
    pip install "$PYTHON_TARGET_DEPENDENCIES_PATH"/aclruntime-0.0.2-cp39-cp39-linux_aarch64.whl "$PYTHON_TARGET_DEPENDENCIES_PATH"/ais_bench-0.0.2-py3-none-any.whl

# CANN 环境变量
ENV CANN_TARGET_DEPENDENCIES_PATH="$TARGET_DEPENDENCIES_PATH"/cann
# 安装CANN
RUN source /etc/bash.bashrc && \
    CANN_VERSION=$(jq -r '.CANN_VERSION' "$CANN_TARGET_DEPENDENCIES_PATH/cann.json") && \
    CANN_URL=$(jq -r '.CANN_URL' "$CANN_TARGET_DEPENDENCIES_PATH/cann.json") && \
    wget -P "$CANN_TARGET_DEPENDENCIES_PATH" "$CANN_URL" --no-check-certificate && \
    chmod +x "$CANN_TARGET_DEPENDENCIES_PATH"/Ascend-cann-toolkit_"$CANN_VERSION"_linux-aarch64.run && \
    "$CANN_TARGET_DEPENDENCIES_PATH"/Ascend-cann-toolkit_"$CANN_VERSION"_linux-aarch64.run --quiet --full && \
    printf "# cann environment\nsource /usr/local/Ascend/ascend-toolkit/set_env.sh\n" >> /etc/bash.bashrc

# 删除目标依赖路径
RUN rm -rf "$TARGET_DEPENDENCIES_PATH"

# 添加镜像启动命令，启动ssh服务，nginx服务
ENTRYPOINT service ssh restart && bash
